<template>
	<view class="">
		<u-navbar title="我的礼物" :autoBack="true" bgColor="#fff" :fixed="false"></u-navbar>
		<view class="p-30">
			<template v-if="!noData">
				<view class="card p-30 flex-row align-center justify-between" :class="{'mt-30' : index != 0}" v-for="(item,index) in list">
					<view class="flex-row align-center">
						<u-image :src="item.gift.image" width="100rpx" height="100rpx"></u-image>
						<view class="ml-10">
							<view class="">
								{{item.gift.name}}
							</view>
							<view class="gray-color small-size mt-10">
								{{$u.timeFormat(item.createtime, 'yyyy年mm月dd日')}}
							</view>
						</view>
					</view>
					<view class="price-color">
						+ {{item.money}}元宝
					</view>
				</view>
			</template>
			<template v-else>
				<u-empty mode="data">
				</u-empty>
			</template>
		
		</view>
	</view>
</template>

<script>
	import {myGiftList} from '@/common/api.js'
	import {
		mapState
	} from "vuex";
	import commonMixin from '@/mixins/common.js';
	export default {
		mixins: [commonMixin],
		data() {
			return {
				status: 'loading',
				list: [],
				noData: false,
				page: 1,
				total: 0
			}
		},
		onLoad() {
			this.init();
		},
		methods: {
			init() {
				this.status = "loading";
				this.list = [];
				this.page = 1;
				this.noData = false;
				this.getList();
			},
			getList() {
				let vm = this;
				let params = {
					'page': this.page,
					'limit': this.$u.config.limit,
				};
				myGiftList({
					params
				}).then(res => {
					console.log(res.length);
					if (res.rows.length != 0) {
						this.noData = false;
						this.list = this.list.concat(res.rows);
						this.total = res.total;
						this.page++;
						this.status = "loadmore"
					} else {
						this.noData = true;
						this.status = "nomore"
					}
				}).catch(err => {
					vm.$refs.uToast.show({
						type: 'error',
						message: err.message,
						complete() {
							vm.loading = false;
						}
					})
				})
			},
			//上拉
			onPullDownRefresh() {
				this.status = "loading"
				this.init();
				setTimeout(function() {
					uni.stopPullDownRefresh();
				}, 500);
			},
			//下拉
			onReachBottom() {
				let vm = this;
				this.status = 'loading';
				//延时为了看效果
				setTimeout(() => {
					if (vm.page > Math.ceil(vm.total / this.$u.config.limit)) {
						this.status = 'nomore';
					} else {
						vm.getList();
					}
				}, 200)
			}
		}
	}
</script>

<style lang="scss" scoped>
</style>